import { Component, OnInit, Input } from '@angular/core';
import {ApiService} from "@core/services/http/api.service";
import {DatePipe} from "@angular/common";
import {ApesMessageService} from "@shared/apes/message";

@Component({
  selector: 'car-details',
  templateUrl: './car-details.component.html',
  styleUrls: ['./car-details.component.scss']
})
export class CarDetailsComponent implements OnInit {
  @Input()
  set clickKey(value: any) {
    this._clickKey = value;
    this.oddNumber = value['JJCDH'];
    this.carNumber = value['CPHM'];
    this.mdId = value['MDID'];
  }
  get clickKey() {
    return this._clickKey;
  }

  @Input()
  set parmas(data: any) {
    this._parmas = data;
  }
  get parmas() {
    return this._parmas;
  }

  _parmas: any[] = [];
  _clickKey: any;
  carNumber: string;          // 车牌号
  oddNumber: any;             // 交接车单号
  mdId: any;

  rowData:any[] = [];
  iconData: any[] = [];       // 接车检测数据
  gridStyle = {
    padding: 0
  }
  visible: string = 'none';
  title: string;
  rightList = {};
  topData: any[];
  bisi: string = 'block';
  total: string;

  constructor( private apiService:ApiService, private datePipe: DatePipe,
               private msg: ApesMessageService) { }

  ngOnInit() {
  }

  getTopChanges() {
    if (this.bisi === 'block') {
      this.bisi = 'none';
    }else{
      this.getIcon(this.oddNumber,this.carNumber,this.mdId);
      this.bisi = 'block';
    }
  }

  getChange(data: any){
    this.total = data['title']
    let tit = data['title'].slice(0,4);
    if(this.title === tit){
      if(this.visible === 'block'){
        this.rowData = [];
        this.visible =  'none';
        this.total = '';
      }else {
        this.visible = 'block';
      }
    }else {
      this.visible = 'block'
    }
    this.title = data['title'].slice(0,4);
    this.getData(data["api"]);
  }

  getTablesData( oddNum: any, carNum: any, mdid: any ) {
    let data = {
      'interfaceMethod': 'ipmCLZKForAppService.getLeftTotal', 'pdata': {
        'JJCDH': oddNum, 'CPHM': carNum, 'MDID': mdid
      }
    };
    this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            this.rightList = data[0];
            this.topData = this.setRightData();
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

  // 脑袋数据
  setRightData() {
    return [
      {
        rightList: [
          {lable: '项目', feild: 'FWLXMC'},
          {lable: '定损进度', feild: 'DSJDMC'},
          {lable: '配件进度', feild: 'PJJDMC'},
          {lable: '年审进度', feild: 'NSJDMC'},
          {lable: '车在哪', feild: 'CARPLACEMC'},
          {lable: '返修车', feild: 'FXCMC'}
        ]
      }
    ]
  }

  // 接车检测
  getIcon(oddNum: any, carNum: any, mdid: any ) {
    let data = {'interfaceMethod': 'ipmCLZKForAppService.getJCJC', 'pdata': {'JJCDH': oddNum, 'CPHM': carNum, 'MDID': mdid, 'ZT': '1' }};
    this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            const data = JSON.parse(result.obj);
            this.iconData = data;
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

  getHead(list?: any, num?: any, carNum?: any, mdid?: any ) {
    if (list) {
      let data = {'interfaceMethod': list, 'pdata': {'JJCDH': num, 'CPHM': carNum, 'MDID': mdid, 'ZT': '1'}};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const times = JSON.parse(result.obj);
              this._parmas.forEach(
                (v) =>{
                    if(v.headApi === list) {
                       v['ZHS'] = times[0] && times[0].ZHS ? this.toHour(times[0].ZHS) : "0分钟";
                    }
                 }
              );
            }else {
              this._parmas.forEach(
                (v) =>{
                  if(v.headApi === list) {
                    v['ZHS'] =  "0分钟";
                  }
                }
              );
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  getData(list?: any) {
    if (list) {
      let data = {'interfaceMethod': list, 'pdata': {'JJCDH': this.oddNumber, 'CPHM': this.carNumber, 'MDID': this.mdId, 'ZT': '1'}};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              for (let i = 0, len = data.length; i < len; i++) {
                if (data[i]['WCBJ'] === 1) {
                  data[i]['WCBJ'] = '√';
                } else {
                  data[i]['WCBJ'] = '×';
                }
              }
              if (data.DETAILS == undefined) {
                this.rowData = data;
              } else {
                this.rowData = data.DETAILS;
              }
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  // 时间转换（0'00"）
  toHour(data) {
    let day = Math.floor(data / 1440);
    let hour = day >= 0
      ? Math.floor((data - (day * 1440) ) / 60)
      : Math.floor(data / 60);
    let minute = hour >= 0
      ? Math.floor(data - (day * 1440) - (hour * 60) )
      : Math.floor(data / 60 / 24);

    let time = "";
    if (day > 0) {
      // time = day + "天" + hour + "小时";
      time = day + "天" + hour + "小时";
    }else if(hour > 0){
      time = hour + "小时" + minute + "分钟";
    }else if(minute > 0){
      time = minute + "分钟";
    }
    return time;
  }

  showTime(data) {
    let a = data >= 0 ? '' : '-';
    data = data >= 0 ? data : -data;

    let day = Math.floor(data / 1440);
    let hour = day >= 0
      ? Math.floor((data - (day * 1440) ) / 60)
      :Math.floor(data / 60);
    let minute = hour >= 0
      ? Math.floor(data - (day * 1440) - (hour * 60) )
      : data < 0
        ? data
        : Math.floor(data / 60 / 24);

    let time = "";
    if ( day > 0 ) {
      time = day + "天";
    }else if( hour ){
      time = hour + "小时";
    }else if( minute ){
      time = minute + "分钟";
    }
    return time;
  }
}
